package com.fromfuture.xuhuidoc.android.activity.mainframe;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.fromfuture.xuhuidoc.R;
import com.fromfuture.xuhuidoc.android.activity.LoginActivity;
import com.fromfuture.xuhuidoc.android.activity.MyYuYueActivity;
import com.fromfuture.xuhuidoc.android.base.AppManager;
import com.fromfuture.xuhuidoc.android.base.BaseFragment;
import com.fromfuture.xuhuidoc.android.base.BaseWebActivity;
import com.fromfuture.xuhuidoc.android.base.MyWebview;
import com.fromfuture.xuhuidoc.android.commonresouce.MyUrl;
import com.fromfuture.xuhuidoc.android.utils.GlideCircleTransform;
import com.fromfuture.xuhuidoc.android.utils.MySharedPreferencesUtils;

import app.auto.runner.base.SPrefUtil;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by hp on 2017/8/17.
 */

public class MineFragment extends BaseFragment {
    @BindView(R.id.mine_fragment_original_name)
    TextView mine_fragment_original_name;
    @BindView(R.id.mine_fragment_original_tx)
    ImageView mine_fragment_original_tx;

    private Unbinder unbinder;
    private View rootview;
    private MyWebview webview;
    MainFrameActivity mainFrameActivity;

    public MineFragment() {
    }

    @SuppressLint("LongLogTag")
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.e("WorkBenchFragmentMine -->", "onCreateView");
        rootview = inflater.inflate(R.layout.fragment_mine, container, false);
        unbinder = ButterKnife.bind(this, rootview);
        mainFrameActivity = (MainFrameActivity) getActivity();
        init();

        if (SPrefUtil.getValue("G_WEBBASE").toString().equals("test")) {
            ((TextView) rootview.findViewById(R.id.ceshi)).setText("测试版");
            ;
        } else {
            ((TextView) rootview.findViewById(R.id.ceshi)).setText("正式版");
        }
        rootview.findViewById(R.id.ceshi).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyUrl.change((Activity) v.getContext());
            }
        });
        return rootview;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    @OnClick(R.id.mine_fragment_mine_YuYue)
    void mine_fragment_mine_YuYue() {
        Intent intent = new Intent(getActivity(), MyYuYueActivity.class);
        intent.putExtra("type", "我的预约");
        startActivityForResult(intent, 03);
    }

    @OnClick(R.id.mine_fragment_consult_record)
    void mine_fragment_consult_record() {
        Intent intent = new Intent(getActivity(), BaseWebActivity.class);
        intent.putExtra("type", AppManager.type_inquiry_record);
        startActivityForResult(intent, AppManager.type_inquiry_record);
    }

    @OnClick(R.id.mine_fragment_changedPwd)
    void mine_fragment_changedPwd() {
        Intent intent = new Intent(getActivity(), BaseWebActivity.class);
        intent.putExtra("type", AppManager.type_change_pwd);
        startActivityForResult(intent, AppManager.type_change_pwd);
    }

    @OnClick(R.id.mine_fragment_out_login)
    void mine_fragment_out_login() {
        MySharedPreferencesUtils.putValue(getActivity(), "DOCINFO", "DOCPHONE", null);
        MySharedPreferencesUtils.putValue(getActivity(), "DOCINFO", "DOCNAME", null);
        MySharedPreferencesUtils.putValue(getActivity(), "DOCINFO", "DOCGUID", null);
        MySharedPreferencesUtils.putValue(getActivity(), "DOCINFO", "DOCJOBTITLE", null);
        MySharedPreferencesUtils.putValue(getActivity(), "DOCINFO", "DOCMORETX", null);
        MySharedPreferencesUtils.putValue(getActivity(), "DOCINFO", "DOCTX", null);
        MySharedPreferencesUtils.putValue(getActivity(), "DOCINFO", "DOCPWD", null);
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        startActivity(intent);
        try {
            AppManager.mainFrameActivity.finish();
            mainFrameActivity.finish();
        } catch (Exception e) {

        }

    }

    private void init() {
//        if (MySharedPreferencesUtils.getValue(getActivity(),"DOCINFO","DOCGUID","")!=""){
//
//        }
        inituserinfo(true);
    }//

    //显示头像和姓名
    public void inituserinfo(boolean isLogin) {
        Glide.with(getActivity()).load(MyUrl.imgurl + MySharedPreferencesUtils.getValue(getActivity(), "DOCINFO", "DOCGUID", ""))
                .placeholder(R.mipmap.my_header)
                .transform(new GlideCircleTransform(getActivity()))
                .error(R.mipmap.my_header)
                .into(mine_fragment_original_tx);
        if (isLogin) {
            mine_fragment_original_name.setText(MySharedPreferencesUtils.getValue(getActivity(), "DOCINFO", "DOCNAME", "姓名"));
        } else {
            mine_fragment_original_name.setText("姓名");
        }
    }

}
